import data from './lib/cruise-data'

export default class Cruise {
  constructor(basicCesium) {
    this.basicCesium = basicCesium
    this.viewer = basicCesium.viewer;

    this.initTime()
    this.initEntity()
    this.bindEdit()
    //state用于区分当前的状态 0 为删除 1为添加 2为编辑 
    this.state = -1
    this.entity = null
    this.scanEntity = null
  }

  initTime() {
    this.start = Cesium.JulianDate.fromDate(new Date());
    this.viewer.clock.startTime = this.start.clone();
    this.viewer.clock.currentTime = this.start.clone();
    this.viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP; //Loop at the end
    this.viewer.clock.multiplier = 10;
    this.beginAnimate();
  }

  bindEdit() {
    this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
    this.handler.setInputAction((e) => { //单击
      let pick = this.basicCesium.mousePicked(e.position);
      if (pick != undefined && pick.id) {
        let position = pick.id.position.getValue(this.viewer.clock.currentTime)
        // that.vm.$store.dispatch('setCesiumCruiseCallbackStr', {
        //   name: '9527',
        //   position: that.tools.worldToLonlat(position),
        //   cars: 100,
        //   video: 'data/fly.mp4'
        // })
      }

    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }

  initEntity() {
    if (this.entity != null) return false;
    let mills = data.length * 120;
    this.stop = Cesium.JulianDate.addSeconds(this.start, mills, new Cesium.JulianDate());
    this.viewer.clock.stopTime = this.stop.clone();
    this.viewer.timeline.zoomTo(this.start, this.stop);

    let property = new Cesium.SampledPositionProperty();
    data.forEach((d, i) => {
      let time = Cesium.JulianDate.addSeconds(this.start, i * 120, new Cesium.JulianDate())
      let position = this.basicCesium.lnglatToWorld(d[0], d[1], d[2])
      property.addSample(time, position);
    })
    this.entity = this.basicCesium.entityModel(property, {
      model: {
        uri: 'glb/drone.glb',
        minimumPixelSize: 160,
        maximumScale: 20000,
      },
      availability: new Cesium.TimeIntervalCollection([new Cesium.TimeInterval({
        start: this.start,
        stop: this.stop
      })]),
      orientation: new Cesium.VelocityOrientationProperty(property),
      path: {
        resolution: 1,
        material: new Cesium.PolylineGlowMaterialProperty({
          glowPower: 0.2,
          color: Cesium.Color.YELLOW
        }),
        width: 10
      }
    });
    this.scanEntity = this.basicCesium.createScan(this.entity)
  }

  beginAnimate(isTrue = true) {
    this.viewer.clock.shouldAnimate = isTrue;
  }

  reset() {
    if (this.entity == null) return;
    this.viewer.entities.remove(this.entity);
    this.viewer.entities.remove(this.scanEntity);
    this.viewer.clock.currentTime = this.start.clone();
    this.entity = null;
  }
}